﻿using System.Collections.Generic;
using System.Data.Linq;
using Dogshow.Model;
using DogshowRepository;
using System.Linq;

namespace Dogshow.Linq2SQLRepository
{
    public class KinoloziRepository : RepositoryBase, IKinoloziRepository
    {
        public int Sacuvaj(Model.Kinolog kinolog)
        {
            using (var model = new DogshowModel())
            {
                Kinolozi k;
                if (kinolog.Id == 0)
                {
                    k = ModelMapper.Map(kinolog);
                    model.Kinolozis.InsertOnSubmit(k);
                    model.SubmitChanges();
                }
                else
                {
                    k = model.Kinolozis.Single(x => x.Id == kinolog.Id);
                    ModelMapper.Map(kinolog, k);
                    model.SubmitChanges();
                }
                return k.Id; 
            }
        }

        public IEnumerable<Model.Kinolog> VratiSve()
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<Kinolozi>(x => x.Korisnicis);
                dlo.LoadWith<Korisnici>(x => x.Mesta);
                model.LoadOptions = dlo;
                return model.Kinolozis.Select(x => ModelMapper.Map(x)).ToList(); 
            }
        }

        public Model.Kinolog VratiPoId(int id)
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<Kinolozi>(x => x.Korisnicis);
                dlo.LoadWith<Korisnici>(x => x.Mesta);
                model.LoadOptions = dlo;
                var k = model.Kinolozis.SingleOrDefault(x => x.Id == id);
                if (k == null) return null;
                return ModelMapper.Map(k); 
            }
        }

        public IEnumerable<Model.Kinolog> VratiKinologaSaImenom(string deoImena)
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<Kinolozi>(x => x.Korisnicis);
                dlo.LoadWith<Korisnici>(x => x.Mesta);
                model.LoadOptions = dlo;
                return model.Kinolozis.Where(x => x.Korisnicis.Naziv.Contains(deoImena)).Select(ModelMapper.Map).ToArray(); 
            }
        }


        public IEnumerable<Kinolog> VratiKinologeSaStatusom(VrstaKinologa status)
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<Kinolozi>(x => x.Korisnicis);
                dlo.LoadWith<Korisnici>(x => x.Mesta);
                model.LoadOptions = dlo;
                return model.Kinolozis.Where(x => x.Status == (int)status).Select(ModelMapper.Map).ToList(); 
            }
        }

        public int DodajIVratiId(Kinolog kinolog)
        {
            using (var model = new DogshowModel())
            {
                Kinolozi entity = ModelMapper.Map(kinolog);
                model.Kinolozis.InsertOnSubmit(entity);
                model.Submit();
                return entity.Id; 
            }
        }

        public IEnumerable<Kinolog> FiltrirajPoImenu(string filter, int beginIndex, int endIndex)
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<Kinolozi>(x => x.Korisnicis);
                dlo.LoadWith<Korisnici>(x => x.Mesta);
                model.LoadOptions = dlo;
                var svi = model.Kinolozis.AsQueryable();
                if (!string.IsNullOrEmpty(filter))
                {
                    svi = svi.Where(x => x.Korisnicis.Naziv.Contains(filter));
                }
                return svi.Skip(beginIndex).Take(endIndex - beginIndex - 1).Select(ModelMapper.Map).ToArray(); 
            }
        }
    }
}